<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @cancel="onCancel"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { updateUserProfile } from '@/api/user.js'
import dayjs from 'dayjs'
export default {
  props: ['birthday'],
  data() {
    return {
      minDate: new Date(1980, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.birthday),
    }
  },
  methods: {
    onCancel() {
      console.log('1')
      this.$emit('close')
    },
    async onConfirm() {
      this.$toast.loading({
        message: '努力加载中....',
        duration: 0,
      })

      try {
        await updateUserProfile({
          birthday: dayjs(this.currentDate).format('YYYY-MM-DD'),
        })

        this.$toast.success('修改成功')
        this.$emit('close')
        this.$emit('update:birthday', dayjs(this.currentDate).format('YYYY-MM-DD'))
      } catch (error) {
        this.$toast.fail('修改失败')
      }
    },
  },
}
</script>

<style></style>
